<?php
namespace app\common\model;

class ShopPaymentModel extends BaseModel
{
    protected $name = 'shop_payment'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    protected $type = ['pay_config' => 'array'];
    /*读取器*/

    /*修改器*/

    public function getList($siteId){
        $map['site_id'] = $siteId;
        $device                    = is_mobile();
        if($device==3){ //隐藏支付宝
            $map['pay_code'] = ['neq','alipay'];
        }
        $payment = $this->where($map)->where('enabled', 1)->order('pay_sort asc')->select();
        return $payment;
    }
    /**
     * 初始化方法
     * @param $siteId
     */
    public function initData($siteId){
        $info = $this->where('site_id',$siteId)->find();
        if(!$info){
            $dataList=[
                0 => [
                    "pay_code" => "alipay",
                    "pay_name" => "支付宝",
                    "pay_desc" => "",
                    "pay_config" => [
                        "partner" => "2088712112204983",
                        "md5_key" => "649mear295eo40nrpidip14rgzt11w9u",
                        "rsa_private_key" => "",
                    ],
                    "enabled" => 1,
                    "pay_sort" => 1,
                ],
                1 => [
                    "pay_code" => "wxpay",
                    "pay_name" => "微信支付",
                    "pay_desc" => "",
                    "pay_config" => [
                        "app_id" => "",
                        "mch_id" => "",
                        "md5_key" => "",
                        "appsecret" => "",
                    ],
                    "enabled" => 1,
                    "pay_sort" => 2,
                ],
                2 => [
                    "pay_code" => "userpay",
                    "pay_name" => "余额支付",
                    "pay_desc" => "",
                    "enabled" => 1,
                    "pay_sort" => 3,
                ],
                3 => [
                    "pay_code" => "delivery",
                    "pay_name" => "货到付款",
                    "pay_desc" =>  "",
                    "enabled" => 1,
                    "pay_sort" => 4,
                ],
                4 => [
                    "pay_code" => "line",
                    "pay_name" => "线下付款",
                    "pay_desc" => "",
                    "enabled" => 1,
                    "pay_sort" => 5,
                ],
            ]; //初始化数据
            foreach ($dataList as $k=>$v){
                $v['site_id'] = $siteId;
                $new[$k] = $v;
            }
            $this->saveAll($new);
        }
        return true;
    }
}
